//
//  ZMHomeTableViewCell.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/26.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit
import SnapKit
let HomeCellMargin: CGFloat = 10



class ZMHomeTableViewCell: UITableViewCell {

    //记录toolbarview的顶部约束
    var toolbarTopConstranit: Constraint?
    
    
    //懒加载控件
    fileprivate lazy var originalView : ZMHomeOriginalView = ZMHomeOriginalView()
   //数据源属性
    var statusViewModel : ZMStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            
            //移除之前的约束
            toolbarTopConstranit?.deactivate()
            if statusViewModel?.statusesModel?.retweeted_status != nil {
                
                //有转发微博,就要显示转发微博视图
                retweetedView.isHidden = false
                
                //更新toolbarview的约束
                toolBarView.snp.makeConstraints({ (make) in
                    toolbarTopConstranit = make.top.equalTo(retweetedView.snp.bottom).constraint
                })
                
                
                //有转发微博
                retweetedView.statusViewModel = statusViewModel
            }else
            {
                //没有转发微博,那么就隐藏转发微博的视图
                retweetedView.isHidden = true
                toolBarView.snp.makeConstraints({ (make) in
                    toolbarTopConstranit = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }
        }
    }
    //懒加载cell底部toolbar
    fileprivate lazy var toolBarView: ZMHomeToolBarView = ZMHomeToolBarView()
    //懒加载转发微博view
    fileprivate lazy var retweetedView : ZMReweetView = ZMReweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:- 初始化UI
    func setupUI() -> Void {
        
      //  self.backgroundColor = RandowColor()
        
        //加载
        contentView.addSubview(originalView)
        originalView.snp.makeConstraints { (make) in
            make.top.leading.trailing.equalToSuperview()
//            make.height.equalTo(100)
        }
        
        //添加转发微博view
        contentView.addSubview(retweetedView)
        retweetedView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
        }
        
        
        //添加底部toolbar
        contentView.addSubview(toolBarView)
        toolBarView.snp.makeConstraints { (make) in
         toolbarTopConstranit =  make.top.equalTo(retweetedView.snp.bottom).constraint
            make.leading.trailing.equalTo(retweetedView)
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
        
        
    }
    
    
    
    
    
    
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
